package com.itheima.mapper.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.User;
import com.itheima.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserMapperImpl implements UserMapper {
    @Override
    public User select(String username, String password) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //3.调用接口中方法
        User user = userMapper.select(username, password);
        //4.关闭会话
        sqlSession.close();
        return user;
    }

    /**
     * 查询用户名是否存在
     * @param username
     */
    @Override
    public User selectByUsername(String username) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //3.调用接口中方法
        User user = userMapper.selectByUsername(username);
        //4.关闭会话
        sqlSession.close();
        return user;
    }

    /**
     * 添加用户
     * @param user
     */
    @Override
    public void add(User user) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.add(user);
        //4.关闭会话
        sqlSession.commit();
        sqlSession.close();
    }
}
